﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
using Data;
namespace DAO
{
    public class NguoiDung_DAO
    {
        public static NguoiDung_DTO GetNguoiDung(string Ten_Dang_Nhap)
        {
            DataTable dt = Data.SQLAccess.ExecuteQuery("select *from NGUOI_DUNG where TEN_DANG_NHAP = '" + Ten_Dang_Nhap +"'");
            NguoiDung_DTO NguoiDung;
            NguoiDung = new DTO.NguoiDung_DTO();
            NguoiDung._Ten_Dang_Nhap = dt.Rows[0]["TEN_DANG_NHAP"].ToString();
            NguoiDung._Mat_Khau = dt.Rows[0]["MAT_KHAU"].ToString();
            NguoiDung._Ma_Nhan_Vien = int.Parse(dt.Rows[0]["MA_NV"].ToString());
            NguoiDung._Quyen = int.Parse(dt.Rows[0]["MA_QUYEN"].ToString());
            return NguoiDung;
        }
        public static List<NguoiDung_DTO> SelectAll()
        {
            List<NguoiDung_DTO> result = new List<NguoiDung_DTO>();
            DataTable dt = Data.SQLAccess.ExecuteQuery("select *from NGUOI_DUNG");
            DTO.NguoiDung_DTO NguoiDung;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                NguoiDung = new DTO.NguoiDung_DTO();
                NguoiDung._Ten_Dang_Nhap = dt.Rows[i]["TEN_DANG_NHAP"].ToString();
                NguoiDung._Mat_Khau = "";
                NguoiDung._Ma_Nhan_Vien = int.Parse(dt.Rows[i]["MA_NV"].ToString());
                NguoiDung._Quyen = int.Parse(dt.Rows[i]["MA_QUYEN"].ToString());
                result.Add(NguoiDung);
            }
            return result;
        }
        public static string Insert(NguoiDung_DTO nd)
        {
            try
            {
                DataTable dt = Data.SQLAccess.ExecuteQuery("insert into NGUOI_DUNG values('" + nd._Ten_Dang_Nhap + "','" + nd._Mat_Khau + "'," + nd._Ma_Nhan_Vien + "," + nd._Quyen + ")");
                return "true";
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
        public static string Delete(NguoiDung_DTO nd)
        {
            try
            {
                return "true";
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
        public static string DoiMatKhau(NguoiDung_DTO nd)
        {
            try
            {
                DataTable dt = Data.SQLAccess.ExecuteQuery("Update NGUOI_DUNG set MAT_KHAU = '" + nd._Mat_Khau + "' where  TEN_DANG_NHAP = '" + nd._Ten_Dang_Nhap + "'");
                return "true";
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
        public static string PhanQuyen(NguoiDung_DTO nd)
        {
            try
            {
                DataTable dt = Data.SQLAccess.ExecuteQuery("Update NGUOI_DUNG set MA_QUYEN = " + nd._Quyen + " where  TEN_DANG_NHAP = '" + nd._Ten_Dang_Nhap + "'");
                return "true";
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
    }
}
